#import cv2 as cv
import os, shutil

def recursive_listdir(path, findSubDir=False):
    '''
    遍历文件夹下所有文件
    '''
    files = os.listdir(path)
    outPathList = []
    outWholePaths = []
    for file in files:
        file_path = os.path.join(path, file)
        wholePath = os.path.join(path, file_path)
        
        if os.path.isfile(file_path):
            outPathList.append(file)
            outWholePaths.append(wholePath)
        elif findSubDir and os.path.isdir(file_path):
          recursive_listdir(file_path)
    return outPathList, outWholePaths
    

def reName(path, fileformat={}):
    '''
    fileformat = {'.png', '.bmp'}
    '''
    pathList, wholePaths = recursive_listdir(path)
    for srcFile in pathList:
        split = os.path.splitext(srcFile)
        print(split)
        fileFmt = split[-1]
        imgName = split[0]
        fmtOk = False
        
        for fmt in fileformat:
            print(fmt, fileFmt)
            if fileFmt != fmt:
                if fileFmt == ".json" and imgName != "mapping_config":
                    print("delete ", srcFile)
                    os.remove(srcFile)
                continue
            else:
                fmtOk = True
                break
        if not fmtOk:
            continue
        if(not os.path.exists(srcFile)):
            print(srcFile, " >>>> not exist")
            return

        #img = cv.imread(srcFile, 0)
        newName = "sn_"+imgName+"_0"+"sn_"+fileFmt
        print(newName)
        os.rename(srcFile, newName)
        #cv.imwrite(newName, img)
    return

if __name__ == "__main__": 
    currentPath = os.getcwd()
    reName(currentPath, {'.png'})